package com.vn.gofriends.fragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.vn.gofriends.R;
import com.vn.gofriends.adapter.DiscoverAdapter;
import com.vn.gofriends.adapter.ViewPagerAdapter01;
import com.vn.gofriends.item.Trip;

@SuppressLint("NewApi")
public class DiscoverFragment extends Fragment {
	int noofsize = 5;
	ViewPager myPager = null;
	int count = 0;
	Timer timer;
	DiscoverAdapter discoverAdapter;
	ArrayList<Trip> tripList;
	GridView gvDiscover;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater
				.inflate(R.layout.discover_layout, container, false);

		// header
		ViewPagerAdapter01 adapter = new ViewPagerAdapter01(getActivity(),
				noofsize);
		myPager = (ViewPager) view.findViewById(R.id.vpHeader);
		myPager.setAdapter(adapter);
		myPager.setCurrentItem(0);

		Thread thread = new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
				}
				timer = new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						getActivity().runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (count <= 5) {
									myPager.setCurrentItem(count);
									count++;
								} else {
									count = 0;
									myPager.setCurrentItem(count);
								}
							}
						});

					}
				}, 500, 3000);
			}
		};

		thread.start();
		// end header
		// get data
		getData();
		// end get data
		gvDiscover = (GridView) view.findViewById(R.id.gvDiscover);
		tripList = new ArrayList<Trip>();
		discoverAdapter = new DiscoverAdapter(getActivity()
				.getApplicationContext(), R.layout.item_discover_layout,
				tripList);
		gvDiscover.setAdapter(discoverAdapter);
		gvDiscover.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Fragment fragment = new ItemMyTripFragment();
				Bundle bundle = new Bundle();
				bundle.putSerializable("KeyTrip", tripList.get(arg2));
				fragment.setArguments(bundle);
				FragmentManager fragmentManager = getFragmentManager();
				fragmentManager.beginTransaction()
						.replace(R.id.frame_container, fragment).commit();
			}
		});
		return view;
	}

	private void getData() {
		ParseQuery<ParseObject> query = ParseQuery.getQuery("Trip");
		query.whereEqualTo("isPublic", true);
		query.orderByAscending("createdAt");
		query.findInBackground(new FindCallback<ParseObject>() {

			@Override
			public void done(List<ParseObject> trips, ParseException e) {
				if (e == null) {
					tripList.clear();
					for (int i = 0; i < trips.size(); ++i) {
						Trip myTrips = new Trip();
						myTrips.setId(trips.get(i).getObjectId());
						myTrips.setDescription(trips.get(i).getString(
								"description"));
						try {
							myTrips.setUserameOwner(trips.get(i)
									.getParseObject("idOwner").fetchIfNeeded()
									.getString("username"));
						} catch (ParseException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						myTrips.setTitle(trips.get(i).getString("title"));
						myTrips.setDateInit(trips.get(i).getDate("dateInit"));
						// myTrips.setCreatedDate(trips.get(i).getDate("createdAt"));
						myTrips.setDateEnd(trips.get(i).getDate("dateEnd"));
						// myTrips.setUpdatedAt(trips.get(i).getDate("updatedAt"));
						tripList.add(myTrips);
					}
					discoverAdapter.notifyDataSetChanged();
				}
				else{
					Log.d("Vien", e.getMessage());
				}
			}
		});
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		timer.cancel();
		super.onPause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
}
